<?php


namespace app\common\model\shopper;

use app\common\model\BaseModel;
use GatewayWorker\Lib\Gateway;
use Lvht\GeoHash;
use app\common\model\village\Village;
use app\common\model\store\Clerk;

/**
 * 模型
 */
class Shopper extends BaseModel
{
    protected $pk = 'id';

    protected $name = 'shopper';
    /**
     * 隐藏字段
     * @var array
     */
    protected $hidden = [
        'app_id',
        'update_time',
    ];

    static $groupkey = ['g_c_','g_s_'];

    /**
     * 获取列表数据
     */
    public function getList($data = null, $status = '')
    {
        $model = $this;
        !empty($status) && $model = $model->where('status', '=', (int)$status);
        isset($data['village_id']) && $data['village_id']!= '' && $model = $model->where('village_id', '=', $data['village_id']);
        isset($data['keywords']) && $data['keywords'] !== '' && $model = $model->where('username|mobile', 'like', "%".$data['keywords']."%");
        $field = '*,village_id village_name';
        $list = $model->order(['id' => 'desc']) -> with(['user'])-> field($field)->paginate($data);
        return $list;
    }


    /**
     * 关联用户表
     */
    public function user()
    {
        return $this->BelongsTo('app\common\model\user\User', 'uid', 'user_id');
    }

    public function getVillageNameAttr($value)
    {
        if($value){
            return (new Village())-> where('id',$value) -> value('name');
        }else{
            return $value;
        }
    }

    /**
     * 详情
     */
    public static function detail($id)
    {
        return (new static())->where('id',$id)->find();
    }


    public static function shopperDetail($uid)
    {
        $field = 'a.*,v.name';
        return (new static()) -> with(['user']) -> alias('a') -> join('shop_village v','a.village_id = v.id') -> field($field) -> where('a.uid',$uid)->find();
    }

    /**
     * 新增记录
     */
    public function add($data)
    {
        $data['password'] = MD5($data['password']);
        $data = $this->createData($data);
        return self::create($data);
    }

    /**
     * 编辑记录
     */
    public function edit($data)
    {
        if(isset($data['password']) && $data['password']){
            $data['password'] = MD5($data['password']);
        }else{
            unset($data['password']);
        }

        return $this->save($this->createData($data));
    }

    /**
     * 创建数据
     */
    private function createData($data)
    {
        $data['app_id'] = self::$app_id;
        return $data;
    }

    /**
     * 门店状态
     */
    public function getStatusAttr($value)
    {
        $status = [0 => '禁用', 1 => '启用'];
        return ['text' => $status[$value], 'value' => $value];
    }


    /**
     * @Exp    推送消息
     * @Author Mr.Z
     * @Date   2024-03-07
     * @param  [type]     $uid  [用户id]
     * @param  string     $msg  [消息，json格式]
     * @param  integer    $type [类型：0个人,1跑腿团队，2特殊跑腿团队]
     * @return [type]           [description]
     */
    public static function messagePush($uid,$msg='',$type=0){
        Gateway::$registerAddress = '127.0.0.1:1238';
        if($type==1){
             Gateway::sendToGroup(self::$groupkey[0].$uid,$msg);
//            $uid22 = self::$groupkey[0].$uid;
//            file_put_contents('client_ids.txt',$uid22.'------------'.date('Y-m-d H:i:s') . "\r\n", FILE_APPEND);
//            file_put_contents('client_ids.txt',var_export($res,true) . "\r\n", FILE_APPEND);
        }elseif($type==2){
            Gateway::sendToGroup(self::$groupkey[1].$uid,$msg);

        }else{
            $client_ids = Gateway::getClientIdByUid($uid); //通过uid 获取client_id 返回的是数组
            if(!$client_ids){
                return ;
            }
            // 向当前client_id发送数据
            foreach ($client_ids as $value){
                Gateway::sendToClient($value, "$msg\r\n");
            }
        }
    }

    public static function getByUser($user)
    {
        $model = new static();
        return $model->where('uid', '=', $user['user_id'])->where('status', '=', 1)->find();
    }


}